package com.mew.game.slots;

import com.mew.common.dc.db.GameRoomService;
import com.mew.common.dc.db.LoginoutService;
import com.mew.common.dc.db.ProfileService;
import com.mew.common.dc.db.WinFreeService;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.enm.ExitType;
import com.mew.common.entity.game.Jackport;
import com.mew.common.entity.game.WinFree;
import com.mew.common.entity.user.Profile;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.rest.game.SlotsEnterRoomRequest;
import com.mew.common.rest.game.SlotsEnterRoomResponse;
import com.mew.common.rest.sas.EnterGameSasRequest;
import com.mew.common.rest.sas.ExitGameSasRequest;
import com.zebra.common.OnlineType;
import com.zebra.common.event.EventBusAsyncCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SlotsEnterRoomService {

    @Autowired
    private WinFreeService winFreeService;
    @Autowired
    private LoginoutService loginoutService;
    @Autowired
    private ProfileService profileService;
    @Autowired
    private GameRoomService gameRoomService;
//    @Autowired
//    private SasFeignService sasFeignService;
    @Autowired
    private AucRedisService aucRedisService;


    public SlotsEnterRoomResponse enterRoom(SlotsEnterRoomRequest request){
        MewSession session = aucRedisService.getSession(request.getUid());
        Profile profile = profileService.getProfileById(request.getUid());
        if (-9 == session.rioid){
            return doEnterRoom(request, session, profile);
        }

        if (request.getType() == 3){
            return doReconnectRoom(request,  session, profile);
        }
        loginoutService.doExitRoom(session, profile, ExitType.Repeat);

        EventBusAsyncCenter.post(EnterGameSasRequest.builder()
                .userId(request.getUid())
                .gameId(request.getGameId())
                .roomId(request.getRoomId())
                .build());
        return doEnterRoom(request, session, profile);

    }

    private SlotsEnterRoomResponse doEnterRoom(SlotsEnterRoomRequest request, MewSession session, Profile profile){
       long inoutRoomId = loginoutService.createSlotsRoomInOut(request.getGameId(), request.getRoomId(), request.getServerId(), session.dtp, profile);
        doOnlineGame(request, inoutRoomId, request.getType());
        return loadInfo(request, inoutRoomId, profile.getGoldNumber());
    }

    private SlotsEnterRoomResponse doReconnectRoom(SlotsEnterRoomRequest request,  MewSession session, Profile profile){
        doOnlineGame(request, session.rioid, 3);
        return loadInfo(request,  session.rioid, profile.getGoldNumber());
    }

    private void doOnlineGame(SlotsEnterRoomRequest request, long roomInoutId, int enterType){
        MewSession other = new MewSession();
        other.gwsid = request.getGatewaySid();
        other.tk = aucRedisService.getToken();
        other.loc = request.getGameId();
        other.gsid = request.getServerId();
        other.onl =  OnlineType.Online.value();

        other.gid =  request.getGameId();
        other.rid = request.getRoomId();

        if (enterType != 3){
            other.initPlayInfo();
            other.rioid = roomInoutId;
        }
        aucRedisService.onlineSession(request.getUid(), other);
    }

    private SlotsEnterRoomResponse loadInfo(SlotsEnterRoomRequest request,  long inoutRoomId, long goldNumber){
       WinFree winFree = winFreeService.getWinFree(request.getUid(), request.getGameId(), request.getRoomId());
        List<Jackport> jackports=gameRoomService.findJackPortByRoomId(request.getRoomId());
       return SlotsEnterRoomResponse.builder()
               .goldNumber(goldNumber)
               .lastLineNumber(winFree.getLastLineNumber())
               .lastIcon(winFree.getLastIcon())
               .inoutRoomId(inoutRoomId)

               .frees(winFree.getFrees())
               .freeCount(winFree.getFreeCount())
               .freeNumber(winFree.getFreeNumber())
               .freeRate(winFree.getFreeRate())
               .freeType(winFree.getFreeType())

               .bonusBibei(winFree.getBounsBibei())
               .bonusChange(winFree.getBounsChange())
               .bonusNumber(winFree.getBounsNumber())
               .bonusRate(winFree.getBounsRate())
               .bonusType(winFree.getBounsType())

               .state(winFree.getState())
               .json(winFree.getJson())

               .lastWinNumber(winFree.getLastWinNumber())
               .lastBetNumber(winFree.getLastBetNumber())
               .jackports(jackports)
               .errorState(200)

               .build();
    }

    public void exitGame(long userId, int exitType){
        MewSession session = aucRedisService.getSession(userId);
        loginoutService.exitRoom(userId, exitType);
        EventBusAsyncCenter.post(ExitGameSasRequest.builder()
                .userId(userId)
                .gameId(session.gid)
                .roomId(session.rid)
                .bet(session.bt)
                .payment(session.pmt)
                .build());
    }
}
